package com.wswy.wzcx.ui.car.result;

import android.support.annotation.Nullable;
import com.che.libcommon.api.ApiException;
import com.che.libcommon.api.BaseResult;
import com.che.libcommon.utils.optional.Optional;
import com.wswy.wzcx.model.resp.TrafficViolationResp;
import io.reactivex.FlowableSubscriber;
import io.reactivex.observers.ResourceObserver;
import java.io.IOException;
import org.reactivestreams.Subscription;
import timber.log.Timber;

public  class QueryResultObserver implements FlowableSubscriber<Optional<TrafficViolationResp>> {

  @Override
  public void onNext(Optional<TrafficViolationResp> trafficViolationRespOptional) {

    TrafficViolationResp resp = trafficViolationRespOptional.orNull();
    onApiResult(resp);

    Timber.e("--onNext--");
  }

  @Override
  public void onError(Throwable e) {

    Timber.e("--onError--");
    Timber.e(e);

    if(e instanceof ApiException){
      onApiError(((ApiException) e).getBaseResult());
    }else {
      BaseResult result = new BaseResult();
      if(e instanceof IOException){
        result.msg = "网络错误";
        result.statusCode = 4000;
      }
      result.error=e;
      onApiError(result);
    }
  }

  @Override
  public void onComplete() {
    Timber.e("--onComplete--");
  }

  protected void onApiError(@Nullable BaseResult result){

  }
  protected void onApiResult(@Nullable TrafficViolationResp t){

  }

  @Override
  public void onSubscribe(Subscription s) {
    Timber.e("--onSubscribe--"+s);
    s.request(5);
  }
}
